package com.ruiysoft.conf.sms;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.DefaultManagedTaskScheduler;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

/**
 * Created by 化增光 on 2019/2/22.
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        //注册一个名字为"micro-websocket" 的endpoint,并指定 SockJS协议;添加一个服务端点，来接收客户端的连接
        stompEndpointRegistry.addEndpoint("/gs-micro-websocket").addInterceptors(new HttpHandshakeInterceptor()).setAllowedOrigins("*");
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/user")
                .setHeartbeatValue(new long[]{10000L, 10000L})
                .setTaskScheduler(new DefaultManagedTaskScheduler());;//客户端订阅地址的前缀信息
        registry.setApplicationDestinationPrefixes("/api");//客户端给服务端发消息的地址的前缀
        registry.setUserDestinationPrefix("/user");
    }
}
